package org.catdroid.bomba.client;

import java.util.Collections;

import org.catdroid.bomba.classes.Players;
import org.catdroid.bomba.classes.adapters.PlayerListAdapter;
import org.catdroid.bomba.client.AsyncDownloadPlayers.OnGameStartedListener;
import org.catdroid.bomba.controller.Controller;
import org.catdroid.bomba.controller.Controller.OnListUpdatedListListener;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.ToggleButton;

public class GameListActivity extends Activity implements OnClickListener, OnListUpdatedListListener, OnGameStartedListener {
	private ListView list;
    private Context ctx;
    private Boolean jugador;
    private Controller controller;
    private Players players;
    
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_list);
    
    ctx = this;
    controller = Controller.getInstance();
    controller.setOnListUpdatedListener(this);
    
    ToggleButton btn = (ToggleButton) findViewById(R.id.btnStartGame);
    btn.setOnClickListener(this);
    list = (ListView) findViewById(R.id.listView);

    Bundle extras = getIntent().getExtras();
    jugador = false;
    if (extras != null && extras.containsKey("PLAYER"))
    {
            jugador = extras.getBoolean("PLAYER");
    }

    if (!jugador)
    {
            btn.setVisibility(View.GONE);
    }

    updateList();
}

    public void onClick(View v) {
            if (v.getId() == R.id.btnStartGame) {
                    controller.setReady(true);
                    updateList();
            }
    }

    private void updateList()
    {
            if (players!=null) {
                    list.invalidate();
            
            PlayerListAdapter adapter = new PlayerListAdapter(this, R.id.textRow, this.players);
            list.setAdapter(adapter);
            }
    }

    public void onGameStarted() {
            
    }

    public void onListUpdated(Players players) {
            this.players=players;
            Collections.reverse(this.players);
            updateList();           
    }

	public void onChange(Players players) {
		// TODO Auto-generated method stub
		
	}
}